CROSS_COMPILE ?= arm-linux-gnueabihf-

CC			  := $(CROSS_COMPILE)gcc
LD 			  := $(CROSS_COMPILE)ld
OBJCOPY		  := $(CROSS_COMPILE)objcopy
OBJDUMP		  := $(CROSS_COMPILE)objdump
CLINK		  := $(CLINK)-lm -lpthread 
COUT		  := ./obj
TARGET		  ?= $(COUT)/oxcV1
CALARM		  := $(CALARM)-Wno-unused-variable -Wno-unused-result
CFLAGS		  := 

INCLUDIRS	  := ./user \
				 ./part \
				 ./main \
				 ./alarm \
				 ./common \
				 ./socket \
				 ./system \
				 ./config \
				 ./protocol



SRCDIRS		  := ./user \
				 ./part \
				 ./main \
				 ./alarm \
				 ./common \
				 ./socket \
				 ./system \
				 ./config \
				 ./protocol


INCLUDE		  := $(patsubst %,-I %,$(INCLUDIRS))

SFILES		  := $(foreach dir ,$(SRCDIRS),$(wildcard $(dir)/*.S))
CFILES		  := $(foreach dir ,$(SRCDIRS),$(wildcard $(dir)/*.c))
HFILES		  := $(foreach dir ,$(INCLUDIRS),$(wildcard $(dir)/*.h))

SFILENDIR	  := $(notdir $(SFILES))
CFILENDIR	  := $(notdir $(CFILES))

SOBJS		  := $(patsubst %,$(COUT)/%,$(SFILENDIR:.S=.o))
COBJS		  := $(patsubst %,$(COUT)/%,$(CFILENDIR:.c=.o))

OBJS		  := $(SOBJS) $(COBJS)

VPATH		  := $(SRCDIRS)

.PHONY:clean


$(TARGET).bin: $(OBJS)
	$(CC) $(CLINK) -o $(TARGET).bin $^

$(SOBJS) : $(COUT)/%.o:%.S
	$(CC) $(CALARM) $(CFLAGS) -c -O2 $(INCLUDE) -o $@ $<

$(COBJS) : $(COUT)/%.o:%.c
	$(CC) $(CALARM) $(CFLAGS) -c -O2 $(INCLUDE) -o $@ $<

clean:
	rm -rf $(COUT)/*.*

print:
#@echo INCLUDE = $(INCLUDE)
#@echo OBJS = $(OBJS)
	@echo header = $(HFILES)
